var Kuo_admin={
    NAME:"admin",
    TITLE:"管理后台",
    SaftTime:60,
    Groups:async function(){
       
        let $db = await db('admingroup');
        let $data = await $db.ZhiCha("name,id").Select();
        let $zhuan = { 0:Kuo_admin.LANG()['admingroup_moren']};
        if($data){
            for(var ii in $data  ){
                var $hhx = $data[ii];
                $zhuan[$hhx['id']] = $hhx['name'];
            }
        }
        return $zhuan;
    },
    Loginok:async function(KUO,$_SESSION){
        
        if( typeof($_SESSION['adminid']) == "undefined" || $_SESSION['adminid'] < 1 ){
           return -1;
        }
        var Path = KUO.Path;
        let $hash = 'safetoken/'+$_SESSION['adminid'];
        let token  = Path[0]?Path[0]:"";
        for(var i in Path ){
            let tmp = Path[i];
            if(tmp.length == 36 && tmp.indexOf("-") >-1 && tmp.split("-").length == 5){
                token = tmp;
            }
        }
        let method = Path.length > 0?Path[Path.length-1]:"get";
        let token_ = await Mem.Get($hash);
        if(token != token_ && KUO.func_ != "hometongji"  && KUO.func_ != "upload"){

            if(!Debug){
                return -2;
            }
        }
        let $ADMIN = await Adminid($_SESSION['adminid']);
        if(!$ADMIN){
            return -3;
        }
        if( $ADMIN['off'] == '0'){
            await Mem.Del("session/"+KUO.sessionid);
            await Kuolog('adminlog',$_SESSION['adminid'],2,"",KUO.func ,KUO.class,KUO.ip);
            return 99;
        }
      
        if( $ADMIN['verifyip'] == '1'){
            if(KUO.sessionid != $ADMIN['sessionid']  ){
                await  Mem.Del("session/"+KUO.sessionid);
                await Kuolog('adminlog',$_SESSION['adminid'],2,$ADMIN['sessionid'],KUO.func ,KUO.class,KUO.ip);
                return 99;
            }
            let $xip = await Mem.Get('adminip/'+$_SESSION['adminid']);
            if( KUO.ip !=  $xip){
                await Mem.Del("session/"+KUO.sessionid);
                await Kuolog('adminlog',$_SESSION['adminid'],2,$xip,KUO.func ,KUO.class,KUO.ip);
                return 99;
            }
        }
        if( KUO.func_ != "hometongji" ){
            if($ADMIN['groups'] >  0){
                let $quanxian = await this.Groupsid($ADMIN['groups'], "jjj");
                if(!$quanxian ){
                    return -4;
                }
                if(typeof( $quanxian[KUO.class]) == "undefined"){
                    return -5;
                }
                if(typeof( $quanxian[KUO.class][KUO.func_]) == "undefined"){
                    return -6;
                }
                let $kongzhi = {'get':0,'add':1,'put':2,'del':3};
                let zicaidan = $quanxian[KUO.class][KUO.func_];
                if(typeof($kongzhi[method]) == "undefined" ){
                    return -7;
                }
                let xia = $kongzhi[method];
                if( typeof(zicaidan[xia]) == "undefined"){
                    if( typeof(zicaidan[Path[0]]) == "undefined"  ){
                        return -8;
                    }
                    zicaidan = zicaidan[Path[0]];
                    if( typeof(zicaidan[xia]) == "undefined"){
                        return -9
                    }
                }   
            }
        }
        return false;

    },
    Groupscaidan:async function($id,sessionid){
        let $db   = await db('features');
        let $data = await $db.Select();
        let $candai  = {};
        if($data){
            for( var $hhh_ in $data){
                let $hhh = $data[$hhh_];
                $candai[$hhh['pluginid']]  = {
                    'name':$hhh['name'],
                    'typeico':$hhh['typeico'],
                    'main':$hhh['main'],
                    'men':JSON.parse($hhh['menuconfig']),
                };
            }
        }

        if($id == 0){
            return $candai;
        }
        let $quanxian = await Kuo_admin.Groupsid($id, "jjj");
        if($quanxian){
            await Mem.Put("session/"+sessionid,{adminquanxian:$quanxian},Expiredseconds);
        }
        for( var $key  in $candai ){
            let $vvv = $candai[$key];
            if(typeof( $quanxian[$key]) != "undefined"){
                for( var $kk  in $vvv['men']  ){
                    let $dongzuo = $vvv['men'][$kk];
                    if( typeof( $quanxian[$key][$kk]) == "undefined" ){
                        if($candai[$key]['men']){
                            delete($candai[$key]['men'][$kk]);
                        }
                    }else{
                        if(typeof($dongzuo['men']) != "undefined"){
                            for(var  $kk_1 in $dongzuo['men'] ){
                                let $dongzuo_1 =$dongzuo['men'][$kk_1];
                                if( typeof( $quanxian[$key][$kk][$kk_1]) == "undefined"  ){
                                    if($candai[$key]['men'][$kk]['men']){
                                        delete($candai[$key]['men'][$kk]['men'] [$kk_1]);
                                    }
                                }else{
                                    if(typeof($dongzuo_1['men']) != "undefined"){
                                        for( var $kk_2 in $dongzuo_1['men']){
                                            let $dongzuo_2 = $dongzuo_1['men'][$kk_2];
                                            if( typeof( $quanxian[$key][$kk][$kk_1][$kk_2]) == "undefined" ){
                                                if($candai[$key]['men'][$kk]['men'][$kk_1]['men']){
                                                    delete($candai[$key]['men'][$kk]['men'][$kk_1]['men'][$kk_2]);
                                                }
                                            }else{
                                                if(typeof($dongzuo_2['men']) != "undefined"){
                                                    for( var $kk_3 in $dongzuo_2['men']){
                                                        let $dongzuo_3 = $dongzuo_2['men'][$kk_3];
                                                        if( typeof( $quanxian[$key][$kk][$kk_1][$kk_2][$kk_3]) == "undefined" ){
                                                            if($candai[$key]['men'][$kk]['men'][$kk_1][$kk_2]['men']){
                                                                delete($candai[$key]['men'][$kk]['men'][$kk_1]['men'][$kk_2]['men'][$kk_3]);
                                                            }
                                                        }else{
                                                            if(typeof($dongzuo_3['men'])!= "undefined"){
                                                                for( var $kk_4 in $dongzuo_3['men']  ){
                                                                    if( typeof( $quanxian[$key][$kk][$kk_1][$kk_2][$kk_3][$kk_4]) == "undefined"){
                                                                        if($candai[$key]['men'][$kk]['men'] [$kk_1]['men'][$kk_2]['men'][$kk_3]['men']){
                                                                            delete($candai[$key]['men'][$kk]['men'] [$kk_1]['men'][$kk_2]['men'][$kk_3]['men'][$kk_4]);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                delete($candai[$key]);
            }
        }
        return $candai;
    },
    Groupsid:async function($id,$name){
        if(!$name){
            $name = "";
        }
        if($name == ""){
            if($id < 1){
                return Kuo_admin.LANG()['admingroup_moren'];
            }
        }else{
            if($id < 1){
                return true;
            }
        }
        let $db   = await db('admingroup');
        let $data = await $db.Where({id:$id}).Find();
        if(!$data ){
            if($name == ""){
                return Kuo_admin.LANG()['admingroup_nodata'];
            }else{
                return false;
            }
        }
        if($name == ""){
            return $data['name'];
        }else{
            return JSON.parse($data['competence']);
        }
    },
    $DATA:{
        code:0,
        data:null,
        msg:"",
        token:""
    },
    cat:async function(kuo,Context,context){
        Context.headers["content-type"] = "image/svg+xml";
        Context.body =await  VCODE(kuo.sessionid);
    }
}